﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ArchiveSearch
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            dateTimePickerAfter.CustomFormat = "MMM dd, yyyy, hh:mm:ss tt";
            dateTimePickerBefore.CustomFormat = "MMM dd, yyyy, hh:mm:ss tt";
            comboBoxRegex.SelectedIndex = 0;
            comboBoxCoMlp.SelectedIndex = 0;
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void checkBoxAfter_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePickerAfter.Enabled = !checkBoxAfter.Checked;

        }

        private void checkBoxBefore_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePickerBefore.Enabled = !checkBoxBefore.Checked;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            int postMin = 0;
            int postMax = 0;

            //check min
            if (string.IsNullOrWhiteSpace(textBoxPostMin.Text))
            {
                postMin = int.MinValue;
            }
            else
            {
                if (!int.TryParse(textBoxPostMin.Text, out postMin))
                {
                    MessageBox.Show("Post # min is not a valid integer.");
                    return;
                }
            }

            //check max
            if (string.IsNullOrWhiteSpace(textBoxPostMax.Text))
            {
                postMax = int.MaxValue;
            }
            else
            {
                if (!int.TryParse(textBoxPostMax.Text, out postMax))
                {
                    MessageBox.Show("Post # max is not a valid integer.");
                    return;
                }
            }

            //disabled after and before and min and max
            if (checkBoxAfter.Checked && checkBoxBefore.Checked && string.IsNullOrWhiteSpace(textBoxPostMin.Text) && string.IsNullOrWhiteSpace(textBoxPostMax.Text))
            {
                var result = MessageBox.Show("There are a ton of threads, I wouldn't really recommend " +
                                             "searching all of them yet. Not only could it waste a " +
                                             "ton of bandwidth for arch.413chan.net, but the sheer " +
                                             "number of posts collected could crash this program. Do " +
                                             "you still want to proceed?", "Warning", MessageBoxButtons.OKCancel);
                if (result == System.Windows.Forms.DialogResult.Cancel)
                    return;
            }

            //make it
            SearchResults sr = new SearchResults(new FilterSet(
                checkBoxAfter.Checked ? DateTime.MinValue : dateTimePickerAfter.Value,
                checkBoxBefore.Checked ? DateTime.MaxValue : dateTimePickerBefore.Value,
                postMin,
                postMax,
                textBoxName.Text,
                textBoxEmail.Text,
                textBoxSubject.Text,
                textBoxComment.Text,
                textBoxFilename.Text,
                textBoxMD5.Text.Trim(),
                textBoxTripcode.Text.Trim(),
                (Board)comboBoxCoMlp.SelectedIndex, //comboBoxCoMlp.SelectedText.Equals("Both") ? Board.Both : (comboBoxCoMlp.SelectedText.Equals("/co/") ? Board.Co : Board.Mlp),
                comboBoxRegex.SelectedIndex == 1
                ));
            sr.Show();
            sr.CollectResults();
        }

        private void comboBoxCoMlp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxCoMlp.SelectedIndex == 0)
            {
                textBoxPostMax.Enabled = false;
                textBoxPostMin.Enabled = false;
            }
            else
            {
                textBoxPostMax.Enabled = true;
                textBoxPostMin.Enabled = true;                
            }
        }
    }
}
